package model

import (
	"lutai/src/schema"
	"lutai/src/service/db"
	"github.com/pkg/errors"
)

// Packet db
type Geography struct {
	db *db.DB
}

// Init 初始化
func (p *Geography) Init(db *db.DB) *Geography {
	db.AddTableWithName(schema.Geography{}, schema.GeographyTable)
	p.db = db
	return p
}

// Query 查询
func (p *Geography) Query(areaType, geographyCode string) ([]*schema.Geography, error) {
	var ps []*schema.Geography
	var value []interface{}
	sql:=""
	switch areaType {
	case "1":
		sql=`SELECT GeographyCode,GeographyName FROM Geography WHERE SUBSTRING(GeographyCode,3,6)='0000' AND Status=1;`
	case "2":
		sql=`SELECT GeographyCode,GeographyName FROM Geography WHERE SUBSTRING(GeographyCode, 1,2)=? AND SUBSTRING(GeographyCode, 3,6)!='0000' AND SUBSTRING(GeographyCode,5,6)='00' AND Status=1;`
		value=append(value,geographyCode[:2])
	case "3":
		sql=`SELECT GeographyCode,GeographyName FROM Geography WHERE SUBSTRING(GeographyCode, 1,4)=? AND SUBSTRING(GeographyCode, 5,6)!='00' AND Status=1;`
		value=append(value,geographyCode[:4])
	default:
		err:=errors.New("没有那个地理类型")
		return ps,err
	}

	_, err := p.db.Select(&ps, sql, value...)
	return ps, err
}

